package com.youlin.config;

import com.youlin.aop.HelloQuartzJob;
import org.quartz.*;
import org.quartz.impl.calendar.HolidayCalendar;
import org.quartz.impl.calendar.WeeklyCalendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenHourDate;

//@Configuration
public class QuartzConfig {

    @Bean
    public WeeklyCalendar weeklyCalendar() {
        WeeklyCalendar weeklyCalendar = new WeeklyCalendar();
        return weeklyCalendar;
    }

    @Bean
    public HolidayCalendar holidayCalendar() {
        HolidayCalendar holidayCalendar = new HolidayCalendar();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse("2020-10-09");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        holidayCalendar.addExcludedDate(date);
        return holidayCalendar;
    }

    @Bean
    public JobDetail jobDetail1() {
        JobKey jobKey1 = new JobKey("helloQuartzJob-1", "helloJobGroup-1");
        return JobBuilder.newJob(HelloQuartzJob.class)
                .usingJobData("jk0", "jobDetail1")
                .withIdentity(jobKey1)
                .storeDurably()
                .build();
    }

    @Bean
    public JobDetail jobDetail2() {
        return JobBuilder.newJob(HelloQuartzJob.class)
                .withIdentity("helloQuartzJob-2", "helloJobGroup-2")
                .usingJobData("jk0", "jobDetail2")
                .storeDurably()
                .build();
    }

    @Bean
    public CronScheduleBuilder myCronSchedule() {
        return cronSchedule("0/5 * * * * ?").withMisfireHandlingInstructionDoNothing();
    }

//    @Bean
    public Trigger triggerKey1(JobDetail jobDetail1, CronScheduleBuilder myCronSchedule) {
        TriggerKey triggerKey1 = new TriggerKey("helloQuartzTrigger-1", "helloTriggerGroup");
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail1)
                .withIdentity(triggerKey1)
                .withPriority(1)
//                .startAt(evenHourDate(null))
                .withSchedule(myCronSchedule)
                .modifiedByCalendar("weeklyCalendar")
                .build();
    }

//    @Bean
    public Trigger triggerKey2(JobDetail jobDetail2, CronScheduleBuilder myCronSchedule) throws SchedulerException {
        TriggerKey triggerKey2 = new TriggerKey("helloQuartzTrigger-2", "helloTriggerGroup");
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail2)
                .withIdentity(triggerKey2)
                .withPriority(10)
                .withSchedule(myCronSchedule)
                .build();
    }

//    @Bean
    public Trigger triggerKey4(JobDetail jobDetail2) {
        TriggerKey triggerKey4 = new TriggerKey("helloQuartzTrigger-4", "helloTriggerGroup");
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder
                .simpleSchedule()
                .repeatSecondlyForTotalCount(3, 3)
                .withMisfireHandlingInstructionIgnoreMisfires();
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail2)
                .withIdentity(triggerKey4)
                .withPriority(10)
                .startAt(new Date(System.currentTimeMillis() + 10000))
                .withSchedule(simpleScheduleBuilder)
                .build();
    }

}
